package com.ecube.test;

import com.alibaba.fastjson.JSON;
import com.ecube.mqtt.MqttUtils;
import com.ecube.protocol.ProtocolHelper;
import com.ecube.protocol.common.DataReportHead;
import com.ecube.protocol.common.DataReportStandard;
import com.ecube.protocol.enums.ExchangeFunctionEnum;
import com.ecube.protocol.exchange.charger.v3.ChgChargeState;
import com.ecube.utils.DateUtils;
import com.ecube.utils.EnumUtils;
import lombok.Builder;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 换电站
 *  充电机-充放电实时状态
 *
 * @author zhuweijun
 * @version 1.0.0
 * @since 2025/8/1 9:56
 */
public class SendExchangeChargeState {

    public static void main(String[] args) throws Exception {
        while (true){
//            sendChargeState("FAT", "100030002", "040205",
//                    Arrays.asList(
//                            ChargerInfo.builder().chgId("4004190001000002").gunList(Arrays.asList(new GunInfo("10010" + DateUtils.getNowDateMs(), 1), new GunInfo("10011" + DateUtils.getNowDateMs(), 2))).build(),
//                            ChargerInfo.builder().chgId("4004190001000004").gunList(Arrays.asList(new GunInfo("10006" + DateUtils.getNowDateMs(), 1))).build()
//                    )
//            );

            sendChargeState("FAT", "100030001", "400411",
                    Arrays.asList(
                            ChargerInfo.builder().chgId("4004111234567892").gunList(Arrays.asList(
                                    new GunInfo("10012" + DateUtils.getNowDateMs(), 2)
                                    //new GunInfo("10013" + DateUtils.getNowDateMs(), 2)
                                    )
                            ).build()
                    )
            );
            Thread.sleep(1000*10);
        }
    }

    /**
     * 发送充放实时状态
     *
     * @param env env  环境
     * @param productCode productCode     产品编号
     * @param deviceCode deviceCode       换电站编号
     * @param chargerList chargerList     充电机编号列表
     */
    public static void sendChargeState(String env, String productCode, String deviceCode, List<ChargerInfo> chargerList){
        ChgChargeState chargeState = new ChgChargeState();
        ExchangeFunctionEnum function = (ExchangeFunctionEnum) EnumUtils.getFunction(chargeState.getClass());
        if(function == null){
            return;
        }
        String pubTopic = productCode + "/" + deviceCode + "/" + function.getDirection() + "/" + function.getType();

        DataReportStandard reportStandard = new DataReportStandard();
        // 设置报文头
        DataReportHead reportHand = ProtocolHelper.getReportHand(productCode, deviceCode, function.getFunction());
        reportHand.setVersion("V3.0.1");
        reportStandard.setHeader(reportHand);

        int cabinetNo = 1;
        List<ChgChargeState> chargeStateList = new ArrayList<>();
        for (ChargerInfo info : chargerList) {
            ChgChargeState tempChargeState = new ChgChargeState();
            // 设置模块数据
            tempChargeState.setModuleList(createModuleList());
            // 设置充电机报文信息
            tempChargeState.setChgID(info.getChgId());
            tempChargeState.setCabinetNo(cabinetNo);
            tempChargeState.setGunList(createGunList(info.getGunList()));
            tempChargeState.setGunNum(tempChargeState.getGunList().size());
            chargeStateList.add(tempChargeState);
            cabinetNo++;
        }
        reportStandard.setDataBody(chargeStateList);
        String reportContent = JSON.toJSONString(reportStandard);
        System.out.println("发送报文内容：" + reportContent);
        MqttUtils.sendMQTT(env, pubTopic, reportContent);

        // 换电站心跳报文
        MqttUtils.sendMQTT(env, productCode + "/" + deviceCode + "/S2M/keepalive", DateUtils.getNowDate());
    }

    /**
     * 初始化，充电枪信息, A,B枪
     *
     * @param gunInfoList gunInfoList
     * @return {@link List<ChgChargeState.GunMeasurement>}
     */
    private static List<ChgChargeState.GunMeasurement> createGunList(List<GunInfo> gunInfoList) {
        List<ChgChargeState.GunMeasurement> gunList = new ArrayList<>();
        for (int i = 1; i <= gunInfoList.size(); i++) {
            GunInfo gunInfo = gunInfoList.get(i - 1);
            ChgChargeState.GunMeasurement gun = new ChgChargeState.GunMeasurement();
            gun.setGunNo(i);
            LocalDateTime localDateTime = LocalDateTime.now().minusMinutes(10);
            gun.setStartTime(DateUtils.localDateTimeToStr(localDateTime));
            gun.setChgTime(600);
            gun.setOrderSn(gunInfo.getOrderSn());
            gun.setState(gunInfo.getState());
            gunList.add(gun);
        }
        return gunList;
    }

    private static List<ChgChargeState.ModuleMeasurement> createModuleList() {
        List<ChgChargeState.ModuleMeasurement> moduleList = new ArrayList<>();
        for (int i = 1; i <= 2; i++) {
            ChgChargeState.ModuleMeasurement module = new ChgChargeState.ModuleMeasurement();
            module.setModuleNo(i);
            module.setGunNo(i);
            moduleList.add(module);
        }
        return moduleList;
    }

    /**
     * 充电机信息
     */
    @Data
    @Builder
    public static class ChargerInfo {

        /**
         * 充电机编号
         */
        private String chgId;

        /**
         * 充电枪
         */
        private List<GunInfo> gunList;
    }

    /**
     * 充电枪信息
     */
    @Data
    public static class GunInfo {
        /**
         * 充放电流水号
         */
        private String orderSn;

        /**
         * 运行状态（1-充电，2-放电）
         */
        private Integer state;

        public GunInfo(String orderSn, Integer state) {
            this.orderSn = orderSn;
            this.state = state;
        }
    }

}
